﻿using ArtMath.Attribute;
using ArtMath.Component.Point;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.Data;
using ArtMath.Helper;
using ArtMath.Resource;
using System;
using System.Drawing;
using ArtMath.CommonType;

namespace ArtMath.Component.Line
{
    /// <summary>
    /// 约束射线
    /// </summary>
    [Serializable]
    public class ConstrainedRay : RayBase
    {
        AngleStringX strAngle;
        public ConstrainedRay(SketchPad pad, string name, string angle, bool addToList = true) : base(pad, name, ToolTypeName.yssx, addToList)
        {
            InPoints = new PointBase[1];
            StrDirectedAngle = new AngleStringX(Pad, angle);
            DirectedAngle = StrDirectedAngle.RadValue;
        }
        public override double NewDirectedAngle
        {
            get
            {
                return StrDirectedAngle.RadValue;
            }
        }
        void Angle_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            SourcePoint.TempMoveLock = true;
            if (Pad.MoveObject_DoMouseMove(this, PointD.Empty))
            {
                ComponentChanged(this);
            }
            else
            {

            }
            Pad.MoveObject_DoMouseUp(this);
            SourcePoint.TempMoveLock = false;
        }
        void Angle_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            SourcePoint.TempMoveLock = true;
            if (Pad.MoveObject_DoMouseMove(this, PointD.Empty))
            {
                ComponentChanged(this);
            }
            else
            {
                Pad.ShowMessage(string.Format(Resources.Translate("FailModify"), OfTranslator("DirectedAngle")), MessageType.Error);
                StrDirectedAngle.SetExpression(e.OldExpression, false);
            }
            Pad.MoveObject_DoMouseUp(this);
            SourcePoint.TempMoveLock = false;
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("DirectedAngle")]
        public AngleStringX StrDirectedAngle
        {
            get { return strAngle; }
            set
            {
                if (strAngle is object)
                {
                    strAngle.ParametersValueChanging -= Angle_ParametersValueChanging;
                    strAngle.ParametersValueChanged -= Angle_ParametersValueChanged;
                    strAngle.ExpressionSetting -= Angle_ExpressionSetting;
                    strAngle.ExpressionSet -= Angle_ExpressionSet;
                }
                strAngle = value;
                if (strAngle is object)
                {
                    strAngle.ParametersValueChanging += Angle_ParametersValueChanging;
                    strAngle.ParametersValueChanged += Angle_ParametersValueChanged;
                    strAngle.ExpressionSetting += Angle_ExpressionSetting;
                    strAngle.ExpressionSet += Angle_ExpressionSet;
                }
            }
        }
        void Angle_ExpressionSetting(StringX sender)
        {
            SourcePoint.TempMoveLock = true;
            Pad.MoveObject_DoMouseDown(this);
            SourcePoint.TempMoveLock = false;
        }

        void Angle_ParametersValueChanging(DocStringX sender)
        {
            SourcePoint.TempMoveLock = true;
            Pad.MoveObject_DoMouseDown(this);
            SourcePoint.TempMoveLock = false;
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible && SourcePoint != null)
            {
                gra.DrawRay(Pad, LinePen, SourcePoint.Pxy, Pad.DegToPDeg(DirectedAngle * Core.Helper.Angle.RToD));
            }
            base.Paint(gra);
        }
        public override void CreateComponents()
        {
            if (SourcePoint == null)
            {
                SourcePoint = Pad.CreatePoint(Resources.JoinLocalizedString("Source", "Point"), SpareCoordinate_);
                SpareCoordinate_ = SourcePoint.Pxy;
                Pad.TrackObj = this;
                base.CreateComponents();
            }
        }
        public override object CopyAndPaste(SketchPad pad)
        {
            ConstrainedRay cr = new ConstrainedRay(pad, Utility.GetNextAvailableName(Name, pad.CheckObjectNameExist), StrDirectedAngle.DirectExpression, LayerItem != null);
            afterNewCopy(cr);
            return cr;
        }
    }
}
